package com.crm.biz.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.crm.model.BizProject;
import com.crm.service.BizProjectService;
import com.crm.util.ValidateUtil;
import com.github.pagehelper.Page;

@Controller
@RequestMapping("/bizProject")
public class BizProjectController {
	
	@Autowired
	private BizProjectService bizProjectService;
	
	@RequestMapping("/index")
	public String index(){
		
		return "configuration/projectLevel";
	}
	
	@RequestMapping("/getAll")
	@ResponseBody
	public Map<String, Object> getAll(HttpServletResponse response,HttpServletRequest request,BizProject t){
		
		Map<String, Object> map = new HashMap<>();
		
		List<BizProject> list = bizProjectService.selectAllBycode(t);
		
		map.put("list", list);
		map.put("status", "success");
		map.put("msg", "获取成功！");
		
		return map;
	}
	
	@RequestMapping("/addNewRecord")
	@ResponseBody
	public Map<String, Object> addNewRecord(BizProject record, String[] brand, String[] school){
		
		Map<String, Object> map = new HashMap<>();
		
		if(ValidateUtil.isZero(record.getProjectType()))
			throw new RuntimeException("项目种类不能为空！");
		
		if(ValidateUtil.isEmpty(record.getFullName()) || ValidateUtil.isEmpty(record.getShortName()))
			throw new RuntimeException("名称不能为空！");
		
		if(!ValidateUtil.isEmpty(record.getProjectId())){
			bizProjectService.updateByPrimaryKey(record);
		}else{
			bizProjectService.insert(record);
		}
		
		map.put("status", "success");
		map.put("msg", "新增成功！");
		
		return map;
	}
	
	@RequestMapping("/deleteRecord")
	@ResponseBody
	public Map<String, Object> deleteRecord(BizProject record){
		
		Map<String, Object> map = new HashMap<>();
		
		record.setDeleteMark(0);
		bizProjectService.updateByPrimaryKey(record);
		
		map.put("status", "success");
		map.put("msg", "删除成功！");
		
		return map;
	}
	
	@RequestMapping("/updateRecord")
	@ResponseBody
	public Map<String, Object> updateRecord(BizProject record){
		
		Map<String, Object> map = new HashMap<>();
		
		bizProjectService.updateByPrimaryKey(record);
		
		map.put("status", "success");
		map.put("msg", "修改成功！");
		
		return map;
	}
	
	@RequestMapping("/getAllOption")
	@ResponseBody
	public Map<String, Object> getAllOption(HttpServletResponse response,HttpServletRequest request, Page<BizProject> page, BizProject t){
		
		t.setEnable(1);
		
		Map<String, Object> map = new HashMap<>();
		
		Page<BizProject> list = (Page<BizProject>) bizProjectService.selectAll(page, t);
		
		map.put("list", list);
		map.put("status", "success");
		map.put("msg", "获取成功！");
		
		return map;
	}
	
}
